package it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.playdialog;

import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.player.BattlePlayer;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.LinkedList;

import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/** */
public class MultipleCheckBox implements ItemListener, ListSelectionListener
{
	/** */
	private final int MIN;
	
	/** */
	private final int MAX;
	
	/** */
	private int actual;
	
//	/** */
//	private LinkedList<JCheckBox> checkBoxes;
	
	private BattlePlayer player;
	
	/** */
//	private LinkedList<E>
	/** */
	public MultipleCheckBox(final int minimum, final int maximum, BattlePlayer player)
	{
		MIN = minimum;
		MAX = maximum;
		
//		seletectedItems = new Component[MAX];
//		checkBoxes = new LinkedList<JCheckBox>();
		this.player = player;
		actual = 0;
	}

	@Override
	public void itemStateChanged(ItemEvent itemEvent) 
	{
		int state = itemEvent.getStateChange();
		
		if(state == ItemEvent.SELECTED)
		{
			if(actual < MAX)
			{
//				seletectedItems[actual] = (Component) itemEvent.getSource();
				++actual;
			}
			else
			{
				((JCheckBox) itemEvent.getSource()).setSelected(false);
				
				// Event is going to reduce the value of "actual" by one, so re-up it.
				++actual;
			}
		}
		else if(state == ItemEvent.DESELECTED)
		{
			--actual;
//			System.out.println("deselezionato");
		}
	}
	
	/** */
	public void add(final JCheckBox component)
	{
//		checkBoxes.add(component);
		component.addItemListener(this);
	}
	
//	/** */
//	public LinkedList<JCheckBox> getSelecteds()
//	{
//		LinkedList<JCheckBox> list = new LinkedList<>();
//		for (JCheckBox component : checkBoxes) 
//		{
//			if(component.isSelected())
//			{
//				list.add(component);
//			}
//		}
//		
//		return list;
//	}

	/**
	 * @return the mIN
	 */
	public int getMIN() 
	{
		return MIN;
	}
	
	/** */
	public boolean isOkState()
	{
		return actual <= MAX && actual >= MIN;
	}

	@Override
	public void valueChanged(ListSelectionEvent e) 
	{
		if(!e.getValueIsAdjusting() && e.getFirstIndex() != -1)
		{
			int[] selecteds = ((JList<JListCharacterData>)e.getSource()).getSelectedIndices();
			ListModel<JListCharacterData> model = ((JList<JListCharacterData>)e.getSource()).getModel();
			for(int index = 0; index < selecteds.length; index++)
				{
					boolean selected = !model.getElementAt(selecteds[index]).getCheckBox().isSelected();
					model.getElementAt(selecteds[index]).getCheckBox().setSelected(selected);
					
					if(selected)
					{
						player.addCharacter(model.getElementAt(selecteds[index]).getSubject());
					}
					else
					{
						player.removeCharacter(model.getElementAt(selecteds[index]).getSubject());
					}
				}
			((JList<JListCharacterData>)e.getSource()).clearSelection();
		}
		
	}
}